package top.hongthorn.controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.hongthorn.pojo.User;
import top.hongthorn.service.UserService;
import top.hongthorn.util.Result;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 泓宇网络科技工作室
 * @since 2023-12-23 21:11:10
 */

@RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "UserController")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query", example = "admin", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query", example = "1", required = true),
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(String username, String password) {
        return userService.login(username, password);
    }


    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query", required = true),
    })
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(String username, String password) {
        return userService.register(username, password);
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "旧密码", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "string", paramType = "query", required = true),
    })
    @RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST)
    public Result updateUserPassword(String username, String password, String newPassword) {
        return userService.updateUserPassword(username, password, newPassword);
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Result addUser(@ApiParam User user) {
        return userService.addUser(user);
    }

    @ApiOperation(value = "修改用户信息")
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public Result updateUserInfo(@ApiParam User user) {
        return userService.updateUserInfo(user);
    }

    @ApiOperation(value = "用户数据分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", example = "1", required = false),
            @ApiImplicitParam(name = "limit", value = "显示条数", dataType = "Integer", example = "3", required = false),
    })
    @RequestMapping(value = "/userPage", method = RequestMethod.GET)
    public Map userPage(Integer page, Integer limit) {
        return userService.userPage(page, limit);
    }

    @ApiOperation(value = "修改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态值1-2", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query", required = true),
    })
    @RequestMapping(value = "/userState", method = RequestMethod.POST)
    public Result userState(Integer state, Integer id) {
        return userService.userState(state, id);
    }

    @ApiOperation(value = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query", required = true)
    })
    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    public Result deleteUser(Integer id) {
        return userService.deleteUser(id);
    }

}